//
//  FirstTableViewCell.m
//  Homework_19
//
//  Created by 徐昊 on 16/8/10.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "FirstTableViewCell.h"
#import "FirstCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "firstModel.h"
@interface FirstTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *modelArray;
@end
@implementation FirstTableViewCell
- (void)layoutSubviews {
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.contentSize = CGSizeMake(self.contentView.frame.size.width, 0);
    _layout.itemSize = CGSizeMake(_collectionView.contentSize.width / 5, CGRectGetHeight(self.bounds) / 2);
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    [self handleData];
    if (_array) {
        [_collectionView reloadData];
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}
- (void)handleData {
//    NSURLSession *urlSession2 = [NSURLSession sharedSession];
//    NSURL *imageURL = [NSURL URLWithString:@"http://223.99.255.20/cars.app.autohome.com.cn/dealer_v6.0.0/dealer/hotbrands-pm2.json%20HTTP/1.1"];
//    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
//    imageURLRequest.HTTPMethod = @"GET";
//    NSURLSessionDataTask *dataTask = [urlSession2 dataTaskWithRequest:imageURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            NSError *error = nil;
//            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
////            NSLog(@"%@", dic);
//            NSDictionary *result = [dic objectForKey:@"result"];
//            _array = [NSArray array];
//            _array = [result objectForKey:@"list"];
            _modelArray = [NSMutableArray array];
            for (NSDictionary *dic1 in _array) {
                firstModel *model = [firstModel firstWithDictionary:dic1];
                [_modelArray addObject:model];
            }
            
//            [_collectionView reloadData];
//        });
//    }];
//    // 让 dataTask 执行
//    [dataTask resume];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstReuse" forIndexPath:indexPath];
    cell.firstLabel.text = [_modelArray[indexPath.row] name];
    [cell.firstImageView sd_setImageWithURL:[NSURL URLWithString:[_modelArray[indexPath.row] img]]];
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
